package cn.itcast.bos.web.action;

import cn.itcast.bos.constant.Constants;
import cn.itcast.bos.domain.page.PageBean;
import cn.itcast.bos.domain.take_delivery.Promotion;
import com.opensymphony.xwork2.ActionContext;
import com.sun.org.apache.regexp.internal.RE;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.apache.commons.io.FileUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.ws.rs.core.MediaType;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;

@Controller
@ParentPackage("json-default")
@Namespace("/")
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion>{
    //在 bos_fore 项目新建 PromotionAction 提供 pageQuery 方法

    @Action(value = "promotion_pageQuery",results = {@Result(name = "success",type = "json")})
    public String QueryByPage(){
        PageBean<Promotion> bean = WebClient.create("http://localhost:9001/bos_management/services/promotionService" +
                "/pageQuery?page=" + page + "&rows=" + rows)
                .accept(MediaType.APPLICATION_JSON).get(PageBean.class);
        ActionContext.getContext().getValueStack().push(bean);
        return SUCCESS;
    }

    //promotion_showDetail.action
    @Action(value = "promotion_showDetail")
    public String showDetail() throws IOException, TemplateException {
        //判断ID对应html是否存在，存在则直接返回
        String realPath = ServletActionContext.getServletContext().getRealPath("/freemarker");
        File file = new File(realPath + "/" + model.getId()+".html");

        //如果文件不存在，则查询数据库并根据模板生成静态页面
        if (!file.exists()){
            Configuration configuration = new Configuration(Configuration.VERSION_2_3_22);
            configuration.setDirectoryForTemplateLoading(new File(ServletActionContext.getServletContext()
                    .getRealPath("/WEB-INF/freemarker_templates")));
            //获取模板对象
            Template template = configuration.getTemplate("promotion_detail.ftl");
            //动态查询获取网页数据
            Promotion promotion = WebClient.create(Constants.BOS_MANAGEMENT_URL + "/bos_management/services/promotionService/promotion/"
                    + model.getId()).accept(MediaType.APPLICATION_JSON).get(Promotion.class);
            HashMap<String, Object> map = new HashMap<>();
            map.put("promotion",promotion);

            //合并输出
            template.process(map,new OutputStreamWriter(new FileOutputStream(file),"utf-8"));
        }

        //返回静态页面
        ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
        FileUtils.copyFile(file,ServletActionContext.getResponse().getOutputStream());

        return NONE;
    }


}
